﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Repid.Utility.Helper
{
    /// <summary>
    /// 配置管理
    /// </summary>
    public static class ConfigHelper
    {
        /// <summary>
        /// 用户文件路径
        /// </summary>
        public static string FilePath
        {
            get
            {
                string appName = AppSetting("AppName");

                switch (appName)
                {
                    case "Website": return HttpContext.Current.Request.MapPath("~/userfiles");
                    default: throw new Exception("未找到程序类型!");
                }
            }
        }

        /// <summary>
        /// 是否是开发模式
        /// </summary>
        public static bool IsDevelop
        {
            get
            {
                bool isDevelop = false;

                Boolean.TryParse(AppSetting("IsDevelop"), out isDevelop);

                return isDevelop;
            }
        }

        /// <summary>
        /// 连接字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ConnectionString(string key)
        {
            try
            {
                return ConfigurationManager.ConnectionStrings[key].ConnectionString;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 提供程序名称
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ProviderName(string key)
        {
            try
            {
                return ConfigurationManager.ConnectionStrings[key].ProviderName;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 配置文件设置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AppSetting(string key)
        {
            try
            {
                return ConfigurationManager.AppSettings[key];
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}